package com.createidea.scrumfriend.dao.sprint;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.createidea.scrumfriend.dao.BaseDaoImpl;
import com.createidea.scrumfriend.to.ProjectTo;
import com.createidea.scrumfriend.to.SprintTo;
import com.createidea.scrumfriend.to.StoryTo;

public class SprintDaoImpl extends BaseDaoImpl implements  SprintDao {

	@Override
	public List<SprintTo> sprintsByStatus(int status) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateSprint(SprintTo sprint) {
		
		this.getHibernateTemplate().saveOrUpdate(sprint);
		
	}

	@Override
	public SprintTo getSprintById(String sprintId) {
		SprintTo sprintTo = (SprintTo) this.getHibernateTemplate().get(SprintTo.class, sprintId);
		return sprintTo;
	}

	@Override
	public List<SprintTo> getSprintForProject(String projectId) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SprintTo.class);
		detachedCriteria.add(Restrictions.eq("project.id", projectId));
		return this.getHibernateTemplate().findByCriteria(detachedCriteria);
	}

	@Override
	public void createSprint(SprintTo sprint,String projectId) {
		sprint.setProject(new ProjectTo(projectId));
		this.getHibernateTemplate().save(sprint);
	}

	@Override
	public List<SprintTo> getSprintsForProjectByStatus(String projectId,
			int status) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteSprint(String sprintId) {
		SprintTo sprint = new SprintTo();
		sprint.setId(sprintId);
		this.getHibernateTemplate().delete(sprint);
		
	}

	@Override
	public SprintTo getCurrentSprint(String projectId, Date date) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SprintTo.class);
		detachedCriteria.add(Restrictions.eq("project.id", projectId));
		detachedCriteria.add(Restrictions.le("startTime", date));
		detachedCriteria.add(Restrictions.ge("endTime", date));
		List<SprintTo> sprints=this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if(sprints!=null&&sprints.size()>0)
			return sprints.get(0);
		else
			return null;
	}
	
}
